package com.treasure.mall.biz.bo.quickpay;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


/**
 * 微信支付配置信息
 *
 * @author guomw
 */
@Getter
@Setter
@Slf4j
@Component
@ConfigurationProperties(prefix = "pay.wxpay")
public class WechatPayConfig {

    /**
     * appid
     */
    private String appId;

    private String clientAppId;

    /**
     * 商户号
     */
    private String mchId;
    /**
     * 商户秘钥
     */
    private String mchKey;

    /**
     * 异步地址
     */
    private String notifyUrl;

    /**
     * 退款异步地址
     */
    private String refundNotifyUrl;
    /**
     * 交易类型
     */
    private String tradeType = "JSAPI";
    /**
     * 签名类型
     */
    private String signType = "MD5";
    /**
     * p12证书的位置，可以指定绝对路径，也可以指定类路径（以classpath:开头）
     */
    private String keyPath;

    /**
     * @return
     */
    @Bean
    public WxPayService wxPayService() {
        WxPayService wxPayService = new WxPayServiceImpl();
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(this.getAppId());
        wxPayConfig.setMchId(this.getMchId());
        wxPayConfig.setMchKey(this.getMchKey());
        wxPayConfig.setNotifyUrl(this.getNotifyUrl());
        wxPayConfig.setSignType(this.getSignType());
        wxPayConfig.setTradeType(this.getTradeType());
        if (StringUtils.isNotBlank(keyPath)) {
            wxPayConfig.setKeyPath(this.getKeyPath());
        }
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }

    @Bean
    public WxPayService wxPayAppService() {
        WxPayService wxPayService = new WxPayServiceImpl();
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(this.getClientAppId());
        wxPayConfig.setMchId(this.getMchId());
        wxPayConfig.setMchKey(this.getMchKey());
        wxPayConfig.setNotifyUrl(this.getNotifyUrl());
        wxPayConfig.setSignType(this.getSignType());
        wxPayConfig.setTradeType(this.getTradeType());
        if (StringUtils.isNotBlank(keyPath)) {
            wxPayConfig.setKeyPath(this.getKeyPath());
        }
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }

}